// https://nuxt.com/docs/api/configuration/nuxt-config
// @ts-ignore
import devalue from 'devalue'
import appConfig from './config/app.config'
import apiConfig from './config/api.config'
import { isDevMode } from './environment'
// @ts-ignore
// @ts-ignore
export default defineNuxtConfig({
  sourcemap: {
    client: true,
    server: false
  },
  // 开发工具配置
  devtools: {
    enabled: false,
    timeline: {
      enabled: false // 禁用时间线功能减少内存消耗
    }
  },

  // 环境判断配置
  dev: isDevMode,

  // Nitro 服务端配置
  nitro: {
    devProxy: {
      '/dev-api': {
        target: apiConfig.BASE,
        changeOrigin: true,
        secure: false,
        ws: true,
        // @ts-ignore
        rewrite: (path: string) => path.replace(/^\/dev-api/, '')
      }
    }
  },

  // 运行时配置（替代原env配置）
  runtimeConfig: {
    public: {
      apiBase: apiConfig.BASE || '/dev-api',
      socketHost: apiConfig.SOCKET
    },
    // 服务端私有配置
    //  secretKey: process.env.NUXT_SECRET_KEY
  },

  // 数据序列化配置
  transformers: {
    // 生产环境禁用自定义序列化
    ...(apiConfig.dev === 'development' && {
      serialize: devalue,
      revive: (value: string) => eval(`(${value})`)
    })
  },

  // SSR 增强配置
  ssr: true,

  experimental: {
    payloadExtraction: true, // 启用payload提取优化
    // 添加以下配置
    renderJsonPayloads: true,
    asyncContext: true
  },

  // 模块配置
  modules: [
    '@nuxtjs/google-fonts'
  ],

  // Element Plus 优化配置
  elementPlus: {
    autoImport: true,
    importStyle: 'scss', // 强制使用SCSS样式
    themes: ['dark'] // 启用暗黑主题
  },

  // Google Fonts 优化配置
  googleFonts: {
    families: {
      'PingFang SC': [400, 500, 600],
      'Inter': { wght: [400, 500, 600, 700] } // 使用新版语法
    },
    display: 'swap',
    preload: true, // 启用预加载
    download: true // 本地托管字体
  },

  // 插件系统优化
  plugins: [
    '~/plugins/axios.js'
  ],

  // 样式配置优化
  css: [
    'element-plus/theme-chalk/src/index.scss', // 直接引用源文件
    '~/assets/css/main.scss'
  ],

  // 应用元数据配置
  app: {
    head: {
      titleTemplate: '%s | ' + appConfig.meta.title, // 动态标题模板
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width, initial-scale=1, viewport-fit=cover' },
        {
          key: 'description',
          name: 'description',
          content: appConfig.meta.description
        },
        {
          key: 'keywords',
          name: 'keywords',
          content: appConfig.meta.keywords
        }
      ],
      link: [
        {
          rel: 'icon',
          type: 'image/png',
          href: '/favicon.png',
          media: '(prefers-color-scheme: light)'
        }
      ]
    }
  },

  // 构建配置优化
  build: {
    transpile: [
      'element-plus/es',
      'swiper' // 添加常见需要转译的包
    ]
  },

  // 组件自动导入优化
  components: [
    {
      path: '~/components',
      pathPrefix: false,
      extensions: ['.vue'],
      ignore: ['**/*.stories.vue'] // 忽略Storybook文件
    }
  ],

  // Vite 高级配置
  vite: {
    server: {
      hmr: {
        clientPort: 24678,
        protocol: 'ws',
        host: 'localhost',
        overlay: false // 禁用错误覆盖层
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "sass:math";
            @use "@/assets/css/variables.scss" as *;
        
          `
        }
      }
    },
    build: {
      sourcemap: true,
      cssCodeSplit: true, // 启用CSS代码分割
      chunkSizeWarningLimit: 2000 // 调整块大小警告阈值
    }
  },

  // 图片优化模块配置
  image: {
    domains: ['cdn.yourdomain.com'],
    screens: {
      xs: 320,
      sm: 640,
      md: 768,
      lg: 1024,
      xl: 1280
    },
    presets: {
      cover: {
        modifiers: {
          fit: 'cover',
          format: 'webp',
          quality: 80
        }
      }
    }
  },

  compatibilityDate: '2025-04-28'
})
